package com.yiyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiyang.mapper.RoomMapper;
import com.yiyang.pojo.Room;
import com.yiyang.pojo.vo.PageResult;
import com.yiyang.pojo.vo.Result;
import com.yiyang.service.RoomService;
import com.yiyang.utils.BizCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {
    @Autowired
    private RoomMapper roomMapper;

    // 根据id查询房间信息
    @Override
    public Result<Room> getRoomById(Integer id) {
        Room room = this.roomMapper.selectById(id);
        if(room != null){
            return Result.success(BizCode.SUCCESS,room);
        }
        return Result.fail(BizCode.FAIL);
    }

    // 查询楼宇的全部房间信息
    @Override
    public Result<PageResult<List<Room>>> getAllRoom(Integer currentPage, Integer pageSize, String num , String floor) {
        // 创建分页对象，设置分页参数
        Page<Room> page = new Page<>(currentPage,pageSize);
        // 创建条件对象，设置查询条件参数
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(num),"num",num)
                .like(StringUtils.isNotBlank(floor),"floor",floor);
        // 执行查询操作
        this.roomMapper.selectPage(page,wrapper);

        // page.getRecords()获取当前查询到的数据
        if (page.getRecords().size() > 0){
            PageResult<List<Room>> pageResult = new PageResult<>();
            pageResult.setTotal(page.getTotal());
            pageResult.setData(page.getRecords());
            return Result.success(BizCode.SUCCESS,pageResult);
        }else {
            return Result.fail(BizCode.FAIL);
        }

    }

    // 添加房间信息
    @Override
    public Result<Void> addRoom(Room room) {
        if(room.getIsDelete() == null){
            room.setIsDelete(0);
        }
        int i = this.roomMapper.insert(room);
        if(i == 1){
            return Result.success();
        }
        return Result.fail();
    }

    // 修改房间信息
    @Override
    public Result<Void> updateRoom(Room room) {
        int i = this.roomMapper.updateById(room);
        if(i == 1){
            return Result.success();
        }
        return Result.fail();
    }

    // 根据id删除对应房间信息
    @Override
    public Result<Void> deleteRoom(Integer id) {
        int i = this.roomMapper.deleteById(id);
        if(i == 1){
            return Result.success();
        }
        return Result.fail();
    }
}
